package com.oniontech.stamptour.fragment.app;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.oniontech.stamptour.MainActivity;
import com.oniontech.stamptour.R;
import com.oniontech.stamptour.common.adapter.Bean.ContentsListBean;
import com.oniontech.stamptour.fragment.DefaultFragment;
import com.oniontech.stamptour.fragment.listener.FragmentListener;

/**
 * Created by Administrator on 14. 1. 23.
 */
public class WebViewFragmentContDetail extends DefaultFragment implements FragmentListener {
    private ContentsListBean bean = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.webview, null);
        WebView webview = (WebView) view.findViewById(R.id.webView);
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportMultipleWindows(true);
        webview.setVerticalScrollBarEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        webview.requestFocus();
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if(url.endsWith(".mp4")) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    Uri uri = Uri.parse(url);
                    i.setDataAndType(uri, "video/mp4");
                    startActivity(i);
                }
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if(Uri.parse(url).getHost().length() == 0) {
                    return false;
                }

                if(url.endsWith(".mp4")) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    Uri uri = Uri.parse(url);
                    i.setDataAndType(uri, "video/mp4");
                    startActivity(i);
                    return true;
                }else {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                return true;
                }
//                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
        webview.loadUrl(this.bean.getUrl());

        TextView navTitle_txt = (TextView) view.findViewById(R.id.navTitle_txt);
        navTitle_txt.setText(R.string.tit_detail);

        ImageView rightImg = (ImageView) view.findViewById(R.id.rightImg);
        ImageLoader.getInstance().displayImage("assets://img/" + bean.getInfoVisitImg(), rightImg);
        rightImg.setVisibility(View.VISIBLE);

        return view;
    }




    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * On fragment data.
     *
     * @param obj the obj
     */
    @Override
    public void onFragmentData(Object obj) {
        if (obj instanceof ContentsListBean) {
            this.bean = (ContentsListBean) obj;
        }
    }
}
